<?php
class GrouponAddressDSBase extends AbstractDSBase {
	/**	 * The GrouponAddressEntity Object will be created before calling create function	 *	 * @access public	 *	 * @return GrouponAddressEntity object	 *	 */
	public function beforeCreate() {
		// New GrouponAddressEntity		$grouponAddressEntity = new GrouponAddressEntity ();
		$grouponAddressEntity = parent::beforeInsert ( $grouponAddressEntity );
		
		// Return GrouponAddressEntity		return $grouponAddressEntity;
	}
	
	/**	 * The GrouponAddressEntity Object will be getted item from database before calling update function	 *	 * @access public	 *	 * @param int $id: Id of the faq category	 *	 * @return GrouponAddressEntity object	 *	 */
	public function beforeUpdate($id) {
		// Get faq category item		$grouponAddressEntity = $this->getById ( $id );
		
		// Return GrouponAddressEntity		return $grouponAddressEntity;
	}
	
	/**	 * Insert new row to faq category	 *	 * @access public	 *	 * @param GrouponAddressEntity $grouponAddressEntity: object to be inserted	 *	 * @return array of primary key of new inserted row	 */
	public function create($grouponAddressEntity) {
		// New Data Access of faq category		$grouponAddressDataAccess = new GrouponAddressDataAccess ();
		
		// Create faq category and return		return $grouponAddressDataAccess->create ( $grouponAddressEntity );
	}
	
	/**	 * Update an existing row of faq category	 *	 * @access public	 *	 * @param GrouponAddressEntity $grouponAddressEntity: object to be updated	 *	 * @return bool	 */
	public function update($grouponAddressEntity) {
		// New Data Access of faq category		$grouponAddressDataAccess = new GrouponAddressDataAccess ();
		
		// Update faq category item and return		return $grouponAddressDataAccess->update ( $grouponAddressEntity );
	}
	
	/**	 * Delete an existing row of faq category	 *	 * @access public	 *	 * @param int $id: id of row need to be deleted	 *	 * @return bool	 */
	public function del($id) {
		// New Data Access of faq category		$grouponAddressDataAccess = new GrouponAddressDataAccess ();
		$result = false;
		if (! is_null ( $id )) {
			
			// Detele faq category and return			$result = $grouponAddressDataAccess->del ( $id );
		}
		
		return $result;
	}
	
	/**	 * Get faq item	 *	 * @access public	 *	 * @param int $id: Id of faq category	 *	 * @return GrouponAddressEntity object	 */
	public function getById($id) {
		// New Data Access of faq category		$grouponAddressDataAccess = new GrouponAddressDataAccess ();
		
		// Get faq category item and return		return $grouponAddressDataAccess->getById ( $id );
	}
	
	/**	 * Get all rows of faq category table with search criterias	 *	 * @access public	 *	 * @param array $arrElement: array of search criterias	 * @param string $orderby: Field and the way ordered (ASC or DESC)	 * @param int $begin: Begin item	 * @param int $limit: Number rows to query	 *	 * @return array GrouponAddressEntity object	 */
	public function getItems($arrElement, $orderby = null, $begin = null, $limit = null) {
		// New Data Access of faq category		$grouponAddressDataAccess = new GrouponAddressDataAccess ();
		
		// Get all faq category based on search criterias		return $grouponAddressDataAccess->getItems ( $arrElement, $orderby, $begin, $limit );
	}
	
	/**	 * Get total items of faq category	 *	 * @access public	 *	 * @param array $arrElement: array of search criterias	 *	 * @return int total: Num of rows of faq category	 */
	public function getTotals($arrElement) {
		// New Data Access of faq category		$grouponAddressDataAccess = new GrouponAddressDataAccess ();
		
		// Get total items of faq category based on search criterias and return		return $grouponAddressDataAccess->getTotals ( $arrElement );
	}
	
	/**
	 * Get total items of faq category
	 *
	 * @access public
	 *
	 * @param array $addres: string of address 
	 *
	 * @return ArrayIterator 
	 */
	
	public function getlatitude($addres) {
		$opts = array ('http' => array (

		'ignore_errors' => true ) );
		
		$context = stream_context_create ( $opts );
		
		$content = file_get_contents ( "http://maps.googleapis.com/maps/api/geocode/xml?address=" . urlencode ( $addres ) . "&sensor=true", false, $context );
		$data = new SimpleXMLElement ( $content );
		
		$lat = $data->xpath ( 'result/geometry/location/lat' );
		$lng = $data->xpath ( 'result/geometry/location/lng' );
		if (empty ( $lat ) || empty ( $lat [0] )) {
			$lat1 = 0;
		} else {
			$lat1 = $lat [0];
		}
		
		if (empty ( $lng ) || empty ( $lng [0] )) {
			$lng1 = 0;
		} else {
			$lng1 = $lng [0];
		}
		return array ("lat" => $lat1, "lng" => $lng1 );
	}
}